package com.atguigu.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Response;


/**
 * 属性分组
 *
 * @author fk
 * @email hahahahha@gmail.com
 * @date 2020-08-09 23:15:28
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**com.atguigu.gulimail.product.vo.AttrGroupWithAttrsVo
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public Response getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1.查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithattrsByCatelogId(catelogId);

        //2.查出每个属性分组的所有属性

        return Response.ok().put("data",vos);

    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public Response attrNoRelation(@PathVariable Long attrgroupId,
                                   @RequestParam Map<String,Object>params){
       PageUtils pageUtils=  attrService.getNoRelationAttr(params,attrgroupId);
        return Response.ok().put("page",pageUtils);
    }

    /**删除功能
     * /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public Response deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return Response.ok();
    }
    /**
     * http://localhost:88/api/product/attrgroup/attr/relation
     * 新增关联关系
     */
    @PostMapping("/attr/relation")
    public Response addRelation(@RequestBody List<AttrGroupRelationVo>vos){
        relationService.savaRelation(vos);
        return Response.ok();
    }

    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public Response attrRelation(@PathVariable Long attrgroupId){
       List<AttrEntity> entities =  attrService.getRelationAttr(attrgroupId);
       return Response.ok().put("data",entities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
   // @RequiresPermissions("product:attrgroup:list")
    public Response list(@RequestParam Map<String, Object> params,
                         @PathVariable("catelogId") Long catelogId){
      //  PageUtils page = attrGroupService.queryPage(params);
        PageUtils page =attrGroupService.queryPage(params,catelogId);
        return Response.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public Response info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long [] path = categoryService.finCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return Response.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:attrgroup:save")
    public Response save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public Response update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return Response.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public Response delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return Response.ok();
    }

}
